package com.example.class_manage.Controller;

import com.example.class_manage.Entity.Homework_text;
import com.example.class_manage.Mapper.Apprise_HomeWork_mapper;
import com.example.class_manage.Mapper.HomeWork_text_mapper;
import com.example.class_manage.Util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * @author Miao-yongzheng
 * @create 2021-11-11 14:39
 */
@Slf4j
@RequestMapping("/Apprise_HomeWork")
@RestController
@Api(tags = "审阅照片的接口")
public class Apprise_HomeWork_Controller {
    @Autowired
    Apprise_HomeWork_mapper apprise_homeWork_mapper;

    @Autowired
    HomeWork_text_mapper homeWork_text_mapper;

    @PostMapping("/Apprise_pic")
    public JsonData Apprise_pic(
            @ApiParam(value = "评价", required = true) @RequestParam String appraise,
            @ApiParam(value = "作业分数", required = true) @RequestParam String grade,
            @ApiParam(value = "作业id", required = true) @RequestParam String id,
            @ApiParam(value = "学生id", required = true) @RequestParam String stu_id
    ) {
        Integer stute = apprise_homeWork_mapper.Apprise_pic(appraise, grade, id, stu_id);
        return stute != 0 ? JsonData.buildSuccess("提交成功") : JsonData.buildError("提交失败");
    }

    @PostMapping("/Apprise_text")//选择人工审阅
    public JsonData Apprise_text(
            @ApiParam(value = "评价", required = true) @RequestParam String appraise,
            @ApiParam(value = "作业分数", required = true) @RequestParam String grade,
            @ApiParam(value = "作业id", required = true) @RequestParam String id,
            @ApiParam(value = "学生id", required = true) @RequestParam String stu_id,
            @ApiParam(value = "作业答案", required = true) @RequestParam String Answer
    ) {
        Integer stute = apprise_homeWork_mapper.Apprise_text(appraise, grade, Answer, id, stu_id);
        return stute != 0 ? JsonData.buildSuccess("更新成功") : JsonData.buildError("更新失败");
    }

    @PostMapping("/Apprise_text_auto")//选择自动审阅   //此段是垃圾代码!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    public JsonData UpdateAnswer(@RequestParam("Answer") String Answer, @RequestParam("Homework_id") String Homework_id) {
        int statu = homeWork_text_mapper.UpdateText_Answer(Answer, Homework_id);//此段是垃圾代码
        List<Homework_text> howework_texts = homeWork_text_mapper.Find_Homework_byid(Homework_id);//此段是垃圾代码
        howework_texts.forEach(item ->
        {
            int status = apprise_homeWork_mapper.Set_autoAppraise(Homework_id);
            if (status != 0)
                log.info("评价成功");
            if (item.getAnswer().equals(item.getHomework_text())) {
                int stutes = homeWork_text_mapper.Set_grade100(Homework_id, item.getStu_id());
                if (stutes != 0) {
                    log.info("成绩自动匹配成功");
                }
            }
        });
        return statu != 0 ? JsonData.buildSuccess("答案更新成功", 500) : JsonData.buildError("答案更新错误");
    }


}
